﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DesignByFarooq.PowershellByExample.Testing
{
    [TestClass]
    public class MemoryStatusFilePartitionCollectionUnitTest
    {
        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void CreateInstanceWithNullInputFile()
        {
            new MemoryStatusFilePartitionCollection(
                null
                );
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void CreateInstanceWithEmptyInputFile()
        {
            new MemoryStatusFilePartitionCollection(
                string.Empty
                );
        }

        [TestMethod]
        public void CreateInstance()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
                "test.txt"
                );

            Assert.IsNotNull(
                collection
                );

            Assert.AreEqual(
                0,
                collection.Count
                );
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void AddNullItemToCollection()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
                "test.txt"
                );

            collection.Add(
                null
                );
        }

        [TestMethod]
        public void ClearCollection()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
                "test.txt"
                );

            collection.Add(
                new MemoryStatusFilePartition(1, 2)
                );

            Assert.AreEqual(
                1,
                collection.Count
                );

            collection.Clear();

            Assert.AreEqual(
                0,
                collection.Count
                );
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CallContainsWithNullItem()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
                "test.txt"
                );

            collection.Add(
                new MemoryStatusFilePartition(1, 2)
                );

            collection.Contains(
                null
                );
        }

        [TestMethod]
        public void CallContains()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
                "test.txt"
                );

            collection.Add(
                new MemoryStatusFilePartition(1, 2)
                );

            MemoryStatusFilePartition partition = new MemoryStatusFilePartition(
                100,
                200
                );

            collection.Add(
                partition
                );

            Assert.IsTrue(
                collection.Contains(partition)
                );
        }

        [TestMethod]
        [ExpectedException(typeof(NotImplementedException))]
        public void CopyToThrowsException()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
                "test.txt"
                );

            collection.CopyTo(
                null,
                0
                );

        }

        [TestMethod]
        [ExpectedException(typeof(NotImplementedException))]
        public void IsReadOnlyThrowsException()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
                "test.txt"
                );

            bool result = collection.IsReadOnly;
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CallRemoveWithNullItem()
        {
             MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
                "test.txt"
                );

             collection.Remove(
                 null
                 );
        }

        [TestMethod]
        public void CallRemove()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
               "test.txt"
               );

            collection.Add(
                new MemoryStatusFilePartition(1, 2)
                );

            collection.Add(
                new MemoryStatusFilePartition(10, 20)
                );

            Assert.AreEqual(
                2,
                collection.Count
                );

            bool result = collection.Remove(
                new MemoryStatusFilePartition(1, 2)
                );

            Assert.IsTrue(
                result
                );

            Assert.AreEqual(
                1,
                collection.Count
                );

            result = collection.Remove(
                new MemoryStatusFilePartition(10, 20)
                );

            Assert.IsTrue(
                result
                );

            Assert.AreEqual(
                0,
                collection.Count
                );
        }

        [TestMethod]
        public void CallGenericGetEnumerator()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
              "test.txt"
              );

            MemoryStatusFilePartition expectedPartition = new MemoryStatusFilePartition(
                100,
                200
                );

            collection.Add(
                expectedPartition
                );

            IEnumerator<MemoryStatusFilePartition> enumerator = collection.GetEnumerator();

            Assert.IsNotNull(
                enumerator
                );

            int count = 0;

            while (enumerator.MoveNext() != false)
            {
                MemoryStatusFilePartition actualPartition = enumerator.Current;

                Assert.AreEqual(
                expectedPartition,
                actualPartition
                );

                count++;	
            }

            Assert.AreEqual(
                1,
                count
                );
        }

        [TestMethod]
        [ExpectedException(typeof(NotImplementedException))]
        public void NonGemericIEnumerableGetEnumeratorThrowsException()
        {
            MemoryStatusFilePartitionCollection collection = new MemoryStatusFilePartitionCollection(
              "test.txt"
              );

            (collection as IEnumerable).GetEnumerator();
        }

        [TestMethod]
        public void CallFixEndOffsetsWithZeroPartitions()
        {
            MemoryStatusFilePartitionCollection collection = MemoryStatusFile.GetPartitions(
               "ZeroPartitionFile.txt"
               );

            Assert.AreEqual(
                0,
                collection.Count
                );

            collection.FixEndOffsets();

            Assert.AreEqual(
                0,
                collection.Count
                );
        }

        [TestMethod]
        public void CallFixEndOffsets()
        {
            MemoryStatusFilePartitionCollection collection = MemoryStatusFile.GetPartitions(
               "single_partition_dbcc_memorystatus.txt"
               );

            Assert.AreEqual(
                1,
                collection.Count
                );

            Assert.AreEqual(
                0,
                collection[0].StartOffset
                );

            Assert.AreEqual(
                0,
                collection[0].EndOffset
                );

            collection.FixEndOffsets();

            Assert.AreEqual(
                0,
                collection[0].StartOffset
                );

            Assert.AreEqual(
                46493,
                collection[0].EndOffset
                );

        }
    }
}
